﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using NLBS.Entity;
using System.Data;

namespace NLBS.Data
{
    public partial class SqlDataProvider : DbProvider
    {
        public override IList<ExtensionLink> GetExtensionLink(int Type)
        {
            DbCommand dbCommand = db.GetSqlStringCommand("SELECT * FROM dbo.blog_ExtensionLink where Type=@type order by orderID");
            db.AddInParameter(dbCommand, "type", DbType.Int32, Type);
            IList<ExtensionLink> lists = new List<ExtensionLink>();
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                while (dataReader.Read())
                {
                    ExtensionLink link = CommonDataProvider.PopulateExtensionLinkFromIDataReader(dataReader);
                    lists.Add(link);
                }
            }
            return lists;
        }

        public override IList<ExtensionLink> GetExtensionLink()
        {
            DbCommand dbCommand = db.GetSqlStringCommand("SELECT * FROM dbo.blog_ExtensionLink");
            
            IList<ExtensionLink> lists = new List<ExtensionLink>();
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                while (dataReader.Read())
                {
                    ExtensionLink link = CommonDataProvider.PopulateExtensionLinkFromIDataReader(dataReader);
                    lists.Add(link);
                }
            }
            return lists;
        }

        public override int InsertExtensionLink(ExtensionLink link)
        {
            DbCommand dbCommand = db.GetSqlStringCommand(@"INSERT INTO [dbo].[blog_ExtensionLink]  
            ([Name], [Url], [Extensions], [Type],[orderID]) VALUES (@Name, @Url, @Extensions, @Type,@orderID)
            SELECT @linkID=@@IDENTITY");
            db.AddInParameter(dbCommand, "Name", DbType.String, link.LinkName);
            db.AddInParameter(dbCommand, "Url", DbType.String, link.LinkURL);
            db.AddInParameter(dbCommand, "Extensions", DbType.String, link.Extensions);
            db.AddInParameter(dbCommand, "Type", DbType.Int32, link.Type);
            db.AddInParameter(dbCommand, "orderID", DbType.Int32, link.Order);
            db.AddOutParameter(dbCommand, "linkID", DbType.Int32, 10);
            db.ExecuteNonQuery(dbCommand);
            int result = Convert.ToInt32(db.GetParameterValue(dbCommand, "@linkID"));
            return result;
        }

        public override int UpdateExtensionLink(ExtensionLink link)
        {
            DbCommand dbCommand = db.GetSqlStringCommand(@"UPDATE [dbo].[blog_ExtensionLink] 
            SET [Name] = @Name, [Url] = @Url, [Extensions] = @Extensions, [Type] = @Type,orderID=@orderID
            WHERE (([linkID] = @linkID))
            ");
            db.AddInParameter(dbCommand, "Name", DbType.String, link.LinkName);
            db.AddInParameter(dbCommand, "Url", DbType.String, link.LinkURL);
            db.AddInParameter(dbCommand, "Extensions", DbType.String, link.Extensions);
            db.AddInParameter(dbCommand, "Type", DbType.Int32, link.Type);
            db.AddInParameter(dbCommand, "orderID", DbType.Int32, link.Order);
            db.AddInParameter(dbCommand, "linkID", DbType.Int32, link.LinkID);
            return db.ExecuteNonQuery(dbCommand);
        }

        public override int DeleteExtensionLink(int id)
        {
            DbCommand dbCommand = db.GetSqlStringCommand(@"DELETE FROM [dbo].[blog_ExtensionLink] WHERE (([linkID] = @linkID))
            ");
            db.AddInParameter(dbCommand, "linkID", DbType.Int32, id);
            return db.ExecuteNonQuery(dbCommand);
        }

        public override ExtensionLink GetExtensionLinkDetail(int id)
        {
            DbCommand dbCommand = db.GetSqlStringCommand(@"SELECT *
            FROM            blog_ExtensionLink
            WHERE        (linkID = @linkid)");
            db.AddInParameter(dbCommand, "linkID", DbType.Int32, id);

            ExtensionLink link = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    link = CommonDataProvider.PopulateExtensionLinkFromIDataReader(dataReader);
                }
            }
            return link;
        }
    }
}
